package manage.action;

import m.common.action.ActionMeta;
import m.common.action.VuePageResult;
import m.common.action.VueResult;
import m.common.model.util.ModelQueryList;
import m.common.model.util.QueryCondition;
import m.html.VueListResult;
import m.html.element.attr.ButtonType;
import m.html.list.*;
import m.system.exception.MException;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.DictionaryData;
import manage.model.DictionaryType;
import manage.service.DictionaryDataService;
import manage.util.DictionaryUtil;

@ActionMeta(name="manage/dictionaryData")
public class DictionaryDataAction extends StatusAction {

	private DictionaryData model;
	public JSONMessage doSave(){
		JSONMessage result=new JSONMessage();
		try {
			verifyAdminOperPower("manage_system_power");
			getService(DictionaryDataService.class).save(model);
			DictionaryType type=ModelQueryList.getModel(DictionaryType.class,model.getDictionaryType().getOid(),"type");
			DictionaryUtil.clear(type.getType());
			fillJSONResult(result,true,"保存成功");
		} catch (Exception e) {
			fillJSONResult(result,false,e);
		}
		return result;
	}
	
	@Override
	public void doDisableCallback() {
		DictionaryUtil.clearAll();
	}
	@Override
	public void doRecoveryCallback() {
		DictionaryUtil.clearAll();
	}
	public VueResult toEdit() throws Exception {
		verifyAdminOperPower("manage_system_power");
		if(null!=model&&!StringUtil.isSpace(model.getOid())) {
			model=ModelQueryList.getModel(model,"*");
		}else if(null==model){
			throw new MException(this.getClass(),"参数错误");
		}
		return new VuePageResult("page/base/dict/editData.js")
			.addParam("model",model);
	}
	public VueResult dictionayDataList() throws Exception{
		verifyMenu("A1105");
		boolean power=hasAdminOperPower(getStatusPower());
		String name=getParamsValue("parentData.name");
		String value=getParamsValue("parentData.value");
		String title=StringUtil.isSpace(name)?"":(name+" ("+value+") ");
		VueListResult result=getVueListResult(title+"字典值",DictionaryData.class,"action/manage/dictionaryData/dictionayDataList");
		result.addCol(toTextColumn("名称", "name",150,true).setSort(true));
		result.addCol(toTextColumn("值", "value",true));
		result.addCol(toTextColumn("排序", "sort").setSort(ListSortOrder.DESC));
		result.addCol(toStatusColumn(power,true));
		if(power) result.addCol(toSpaceColumn("操作",120,
			ListButton.drawer("修改", ButtonType.PRIMARY,"action/manage/dictionaryData/toEdit",500, SuccessMethod.REFRESH)
				.addParam("model.oid","oid"),
			ListButton.page("子项",ButtonType.LINK,"action/manage/dictionaryData/dictionayDataList")
					.addParam("params[dictionaryType.oid]","dictionaryType.oid").addParam("params[parentData.oid]","oid")
					.addParam("params[parentData.name]","name").addParam("params[parentData.value]","value")
		));
		if(power) result.addButton(
			ListButton.drawer("添加",ButtonType.PRIMARY,"action/manage/dictionaryData/toEdit",500,SuccessMethod.REFRESH)
				.addQueryParam("model.dictionaryType.oid", "dictionaryType.oid").addQueryParam("model.parentData.oid", "parentData.oid")
		);
		result.addSearch(ListSearch.field("","dictionaryType.oid", SearchType.HIDDEN_EQ));
		result.addSearch(ListSearch.field("","parentData.oid",SearchType.HIDDEN_EQ));
		if(StringUtil.isSpace(getParamsValue("parentData.oid"))){
			result.addCondition(QueryCondition.isEmpty("parentData.oid"));
		}
		return result;
	}
	@Override
	public String[] getStatusPower() {
		return new String[]{"manage_system_power"};
	}
	public DictionaryData getModel() {
		return model;
	}
	public void setModel(DictionaryData model) {
		this.model = model;
	}

}
